/*************************************************************************
|                                                                        |
|   CHECK.C                                                     03.02.93 |
|   ADMAKE Utility:  Check structures                                    |
|                                                                        |
*************************************************************************/

#ifdef VMS
#include STDIO
#include "h.h"
#else
#include <stdio.h>
#include "h.h"
#endif

/*************************************************************************
|                                                                        |
|   >>>>  prt                                                            |
|                                                                        |
|   print out the structures as defined in memory - good for checking    |
|   that your make file does what you want (and for debugging make).     |
|                                                                        |
*************************************************************************/

#ifdef PANEWFD
void prt(void)
#else
void prt()
#endif

{
    register struct name *  np;
    register struct depend *dp;
    register struct line *  lp;
    register struct cmd *   cp;
    register struct macro * mp;
    register struct path * pp;

    for (mp = macrohead; mp; mp = mp->m_next)
        fprintf(stderr, "%s = %s\n", mp->m_name, mp->m_val);

    fputc('\n', stderr);

    for (pp = pathhead; pp; pp = pp->p_next)
        fprintf(stderr, "%s = %s\n", pp->p_ext, pp->p_path);

    fputc('\n', stderr);

    for (np = namehead.n_next; np; np = np->n_next)
    {
        if (np->n_flag & N_DOUBLE)
            fprintf(stderr, "%s::\n", np->n_name);
        else
            fprintf(stderr, "%s:\n", np->n_name);
        if (np == firstname)
            fprintf(stderr, "(MAIN NAME)\n");
        for (lp = np->n_line; lp; lp = lp->l_next)
        {
            fputc(':', stderr);
            for (dp = lp->l_dep; dp; dp = dp->d_next)
                fprintf(stderr, " %s", dp->d_name->n_name);
            fputc('\n', stderr);
            for (cp = lp->l_cmd; cp; cp = cp->c_next)
                fprintf(stderr, "-\t%s\n", cp->c_cmd);
            fputc('\n', stderr);
        }
        fputc('\n', stderr);
    }
}

/*************************************************************************
|                                                                        |
|   >>>>  check                                                          |
|                                                                        |
|   recursive routine that does the actual checking                      |
|                                                                        |
*************************************************************************/

#ifdef PANEWFD
static void check(struct name * np)
#else
static void check(np)
struct name * np;
#endif

{
    register struct depend *dp;
    register struct line *  lp;


    if (np->n_flag & N_MARK)
        fatal(MSG[25], np->n_name);     /* circular dependency found */

    np->n_flag |= N_MARK;

    for (lp = np->n_line; lp; lp = lp->l_next)
        for (dp = lp->l_dep; dp; dp = dp->d_next)
            check(dp->d_name);

    np->n_flag &= ~N_MARK;
}


/*************************************************************************
|                                                                        |
|   >>>>  circh                                                          |
|                                                                        |
|   look for circular dependencies                                       |
|                                                                        |
*************************************************************************/

#ifdef PANEWFD
void circh(void)
#else
void circh()
#endif

{
    register struct name *  np;

    for (np = namehead.n_next; np; np = np->n_next)
        check(np);
}

/*************************************************************************
|                                                                        |
|   >>>>  precious                                                       |
|                                                                        |
|   check the target .PRECIOUS, and mark its dependants as precious      |
|                                                                        |
*************************************************************************/

#ifdef PANEWFD
void precious(void)
#else
void precious()
#endif

{
    register struct depend *        dp;
    register struct line *          lp;
    register struct name *          np;


    if (!((np = newname(".PRECIOUS"))->n_flag & N_TARG))
        return;

    for (lp = np->n_line; lp; lp = lp->l_next)
        for (dp = lp->l_dep; dp; dp = dp->d_next)
            dp->d_name->n_flag |= N_PREC;
}

/*************************************************************************
|                                                                        |
|   >>>>  snapshot                                                       |
|                                                                        |
|   check the target .SNAPSHOT, get times for its dependants             |
|                                                                        |
*************************************************************************/

#ifdef PANEWFD
void snapshot(void)
#else
void snapshot()
#endif

{
    register struct depend *        dp;
    register struct line *          lp;
    register struct name *          np;


    if (!((np = newname(".SNAPSHOT"))->n_flag & N_TARG))
        return;

    for (lp = np->n_line; lp; lp = lp->l_next)
        for (dp = lp->l_dep; dp; dp = dp->d_next)
        {
            alltime(dp->d_name->n_name);
        }
}
